package com.sl.config;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public final class CustomThreadFactory implements ThreadFactory {
	private AtomicInteger poolNumber = new AtomicInteger(1);
	private ThreadGroup group;
	private AtomicInteger threadNumber = new AtomicInteger(1);
	private String namePrefix;

	public CustomThreadFactory(String name) {
		SecurityManager s = System.getSecurityManager();
		this.group = (s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup());
		this.namePrefix = (name + "-" + this.poolNumber.getAndIncrement() + "-thread-");
	}

	public Thread newThread(Runnable r) {
		Thread t = new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
		if (t.isDaemon()) {
			t.setDaemon(false);
		}
		if (t.getPriority() != 5) {
			t.setPriority(5);
		}
		return t;
	}
}